//
//  CYHHotOtherVC.m
//  Cat
//
//  Created by dllo on 15/11/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
//

//http://api.huapipi.com/dynamic/l1/detail/2370106?deviceId=1826162&uid=1033268&w=720&loc=collect&channelId=44&h=1280

#import "CYHHotOtherVC.h"
#import "ToolCoreData.h"

@interface CYHHotOtherVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *otherTableView;
@property (nonatomic, strong) NSMutableArray *otherArr;
@property (nonatomic, strong) UIAlertController *alert;
@property (nonatomic, strong) ToolCoreData *toolCoreData;

@end

@implementation CYHHotOtherVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    
    [self createData];
    
    self.toolCoreData = [ToolCoreData defaults];
    
}



#pragma mark - createView
- (void)createView {
    self.otherTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.otherTableView];
    self.otherTableView.delegate = self;
    self.otherTableView.dataSource = self;
    self.otherTableView.separatorStyle = NO;
    
    [self.otherTableView registerClass:[HotCell class] forCellReuseIdentifier:@"other"];
    
    
    
    // 数据异常
    self.alert = [UIAlertController alertControllerWithTitle:@"错误" message:@"作品异常" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self.alert addAction:action];
    
    // 收藏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];
}



#pragma mark - createData
- (void)createData {
    NSString *URL = [NSString stringWithFormat:@"http://api.huapipi.com/dynamic/l1/detail/%@?deviceId=1826162&uid=1033268&w=720&loc=collect&channelId=44&h=1280", self.Secid];
    [ToolNetWorking ToolNetWorking:URL block:^(id result) {
        self.otherArr = [NSMutableArray array];
        
        // 数据异常
        if (result[@"dynamic"] != nil) {
            NSMutableArray *arr = [NSMutableArray arrayWithObject:result[@"dynamic"]];
            self.otherArr = [HotModel BaseModel:arr];
        } else {
            [self presentViewController:self.alert animated:YES completion:nil];
        }
        
        HotModel *model = self.otherArr.lastObject;
        
        // 收藏
        if ([self.toolCoreData isHavePicture:model.content[@"url"]]) {
            self.navigationItem.rightBarButtonItem.title = @"收藏";
        } else {
            self.navigationItem.rightBarButtonItem.title = @"已收藏";
        }
        self.navigationItem.rightBarButtonItem.action = @selector(savePicture:);
        self.navigationItem.rightBarButtonItem.target = self;
        
        [self.otherTableView reloadData];
    }];
}



#pragma mark - tableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.otherArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"other"];
    
    cell.model = self.otherArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数: 文字显示的最大尺寸范围(375, 0)(0 代表最大的意思)
    // 第二个参数:
    CGRect rect = [[self.otherArr[indexPath.row] content][@"word"] boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return VIEW_HEIGHT / 2 + 230 + rect.size.height;
}



#pragma mark - save
- (void)savePicture:(UIBarButtonItem *)button {
    if ([button.title isEqualToString:@"收藏"]) {
        
        [self.toolCoreData savePicture];
        // 收藏
        button.title = @"已收藏";
    }else {
        
        [self.toolCoreData deletePicture];
        // 取消收藏
        button.title = @"收藏";
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
